using Slaks.DocumentSerialization.Dictionary;
using Slaks.DocumentSerialization.Document;
using Slaks.Graphs;
using Slaks.Graphs.Comparators;

namespace Slaks.GraphBasics.Dictionary
{
	/// <summary>
	/// Summary description for BuildDictionary.
	/// </summary>
	public class BuildDictionary
	{
		public static void Build(ref DictionaryMap dictionaryMap, int size, Document document)
		{
			object obj = new object();

			VertexList list = new VertexList();
			foreach (Node node in document.Nodes)
			{
				list.Add(new Vertex(node.Id, node.TotalTermFrequency));
			}
			list.Sort(new FrequencyComparer());

			if (size <= 0 || size > list.Count) size = list.Count;

			for (int i = 0; i < size; i++)
			{
				Vertex vertexItem = list[i];
				long id = vertexItem.GetId();
				if (dictionaryMap.Contains(id) == false) dictionaryMap.Add(id, obj);
			}

		}
	}
}